﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.IDAL;
using SD.Entities;

namespace SD.DAL
{
    public class PremiseDAO : BaseDAO, IPremiseDAO
    {

        #region IPremiseDAO Members

        public List<Premise> GetByFilter(string filterExpression, bool includeInactive)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Get all premises
        /// </summary>
        /// <param name="includeInactive">Indicate whether inactive object will be included or not</param>
        /// <returns>List of Premises</returns>
        public List<Premise> GetAll(bool includeInactive)
        {
            SqlCommand comm = CreateCommand();
            DataTable dt;

            try
            {
                comm.CommandText = "PremiseGetAll";
                comm.Parameters.AddWithValue("@includeInactive", includeInactive);

                dt = ExecuteSelectCommand(comm);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            List<Premise> lstPremises = new List<Premise>();

            foreach (DataRow row in dt.Rows)
            {
                Premise pre = new Premise();
                pre.PremiseID = row[0].ToString();
                pre.LocationName = row[1].ToString();
                pre.KnownAs = row[2].ToString();
                pre.LocationOrg = row[3] == null ? null : new OrganizationDAO().GetById(row[3].ToString());
                pre.LocationStatus = Int32.Parse(row[4].ToString());
                pre.LocationStatusDate = DateTime.Parse(row[5].ToString());
                pre.Address = row[6] == null ? null : new AddressDAO().GetById(row[6].ToString());
                pre.Town = row[7] == null ? null : new TownDAO().GetById(row[7].ToString());
                pre.County = row[8] == null ? null : new CountyDAO().GetById(row[8].ToString());
                pre.Country = row[9] == null ? null : new CountryDAO().GetById(row[9].ToString());
                pre.PrimaryLocation = Boolean.Parse(row[10].ToString());
                pre.LocationManaged = Boolean.Parse(row[11].ToString());
                pre.StNetwork = Boolean.Parse(row[12].ToString());
                pre.Locationtype = row[13] == null ? null : new ReferenceDataDAO().GetById(row[13].ToString());
                pre.LocationDescription = row[14].ToString(); ;
                pre.Phone = row[15].ToString();
                pre.Fax = row[16].ToString();
                pre.Minicomm = row[17].ToString();
                pre.IsNewShop = Boolean.Parse(row[18].ToString());
                pre.ShopFlagDate = DateTime.Parse(row[19].ToString());
                pre.SpecialListShop = Boolean.Parse(row[20].ToString());
                pre.IsActive = Boolean.Parse(row[21].ToString());

                // add to list
                lstPremises.Add(pre);
            }

            return lstPremises;
        }

        /// <summary>
        /// Get all premises by organization
        /// </summary>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public List<Premise> GetByOrgId(string orgId, Boolean includeInactive)
        {
            SqlCommand comm = CreateCommand();
            DataTable dt;

            try
            {
                comm.CommandText = "PremiseGetByOrgId";
                comm.Parameters.AddWithValue("@orgId", orgId);
                comm.Parameters.AddWithValue("@includeInactive", includeInactive);

                dt = ExecuteSelectCommand(comm);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            // add to result list
            List<Premise> lstResult = new List<Premise>();
            foreach (DataRow row in dt.Rows)
            {
                Premise pre = new Premise();
                pre.PremiseID = row[0].ToString();
                pre.LocationName = row[1].ToString();
                pre.KnownAs = row[2].ToString();
                pre.LocationOrg = row[3] == null ? null : new OrganizationDAO().GetById(row[3].ToString());
                pre.LocationStatus = Int32.Parse(row[4].ToString());
                pre.LocationStatusDate = DateTime.Parse(row[5].ToString());
                pre.Address = row[6] == null ? null : new AddressDAO().GetById(row[6].ToString());
                pre.Town = row[7] == null ? null : new TownDAO().GetById(row[7].ToString());
                pre.County = row[8] == null ? null : new CountyDAO().GetById(row[8].ToString());
                pre.Country = row[9] == null ? null : new CountryDAO().GetById(row[9].ToString());
                pre.PrimaryLocation = Boolean.Parse(row[10].ToString());
                pre.LocationManaged = Boolean.Parse(row[11].ToString());
                pre.StNetwork = Boolean.Parse(row[12].ToString());
                pre.Locationtype = row[13] == null ? null : new ReferenceDataDAO().GetById(row[13].ToString());
                pre.LocationDescription = row[14].ToString(); ;
                pre.Phone = row[15].ToString();
                pre.Fax = row[16].ToString();
                pre.Minicomm = row[17].ToString();
                pre.IsNewShop = Boolean.Parse(row[18].ToString());
                pre.ShopFlagDate = DateTime.Parse(row[19].ToString());
                pre.SpecialListShop = Boolean.Parse(row[20].ToString());
                pre.IsActive = Boolean.Parse(row[21].ToString());

                // add to list
                lstResult.Add(pre);
            }

            return lstResult;
        }

        /// <summary>
        /// Get premise by it ID
        /// </summary>
        /// <param name="premiseId">ID of the premise</param>
        /// <returns>Founded Premise. Null if the premise id is not exist.</returns>
        public Premise GetById(string premiseId)
        {
            SqlCommand comm = CreateCommand();

            comm.CommandText = "PremiseGetById";
            comm.Parameters.AddWithValue("@premiseId", premiseId);

            DataTable dt = ExecuteSelectCommand(comm);
            
            Premise pre = null;
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];

                pre.PremiseID = row[0].ToString();
                pre.LocationName = row[1].ToString();
                pre.KnownAs = row[2].ToString();
                pre.LocationOrg = row[3] == null ? null : new OrganizationDAO().GetById(row[3].ToString());
                pre.LocationStatus = Int32.Parse(row[4].ToString());
                pre.LocationStatusDate = DateTime.Parse(row[5].ToString());
                pre.Address = row[6] == null ? null : new AddressDAO().GetById(row[6].ToString());
                pre.Town = row[7] == null ? null : new TownDAO().GetById(row[7].ToString());
                pre.County = row[8] == null ? null : new CountyDAO().GetById(row[8].ToString());
                pre.Country = row[9] == null ? null : new CountryDAO().GetById(row[9].ToString());
                pre.PrimaryLocation = Boolean.Parse(row[10].ToString());
                pre.LocationManaged = Boolean.Parse(row[11].ToString());
                pre.StNetwork = Boolean.Parse(row[12].ToString());
                pre.Locationtype = row[13] == null ? null : new ReferenceDataDAO().GetById(row[13].ToString());
                pre.LocationDescription = row[14].ToString(); ;
                pre.Phone = row[15].ToString();
                pre.Fax = row[16].ToString();
                pre.Minicomm = row[17].ToString();
                pre.IsNewShop = Boolean.Parse(row[18].ToString());
                pre.ShopFlagDate = DateTime.Parse(row[19].ToString());
                pre.SpecialListShop = Boolean.Parse(row[20].ToString());
                pre.IsActive = Boolean.Parse(row[21].ToString());
            }

            return pre;   
        }

        public bool Insert(Premise premise)
        {
            throw new NotImplementedException();
        }

        public bool Update(Premise premise)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string premiseId)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IPremiseDAO Members


        public List<Premise> GetByOrgId(string orgId)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
